<template>
  <div class="search">
    <div class="g-main">
      <div class="search-details">
        <h1 class="page-title">VOCs数据查询</h1>
        <div style="margin-top: 10px; text-align: left; margin-left: 10px">
          <div class="radio-choose" v-if="show">
            <div>(1)选择筛选条件：</div>
            <el-radio-group v-model="radio">
              <el-radio label="催化反应">催化反应</el-radio>
              <el-radio label="大气化学反应">大气化学反应</el-radio>
              <el-radio label="降解菌">降解菌</el-radio>
              <el-radio label="降解菌群">降解菌群</el-radio>
              <!-- <el-radio label="工业应用">工业应用</el-radio> -->
              <el-radio label="焦化行业">焦化行业</el-radio>
              <el-radio label="制药行业">制药行业</el-radio>
              <el-radio label="石油行业">石油行业</el-radio>
              <el-radio label="致癌与非致癌风险">致癌与非致癌风险</el-radio>
              <div></div>
            </el-radio-group>
          </div>
          <div class="sd-title" style="font-size: 18px">
            <span>(2)输入要查询的物质:</span>
          </div>
          <el-tooltip class="item" effect="light" content="输入分子式" placement="bottom-end">
            <el-input placeholder="请输入查询内容如 C6H6" prefix-icon="el-icon-search" v-model="searchVoc"
              class="input-with-select" style="width: 285px">
              <el-button slot="append" @click="search" style="
                  font-size: 16px;
                  color: white;
                  font-family: SourceHanSansCN-Regular;
                  font-weight: normal;
                " type="primary">搜索</el-button>
            </el-input>
          </el-tooltip>
        </div>
      </div>
      <div class="search-result" v-if="voc" v-loading="loading" element-loading-text="数据加载中"
        element-loading-spinner="el-icon-loading">
        <div class="sr-title">
          <p v-if="voc">
            共查询到<span style="color: rgba(0, 84, 190, 1)">{{ resultNum }}</span>条<span
              style="color: rgba(0, 84, 190, 1)">{{ voc }}</span>的结果:
            <span>
              <el-tooltip class="item" effect="light" content="对于每个匹配的结果,将显示以下内容：
           化学物质中文名
            化学物质英文名
            化学物质分子式
            化学物质CASNUM
            化学物质图片(如果存在)" placement="right">
                <i class="el-icon-question"></i>
              </el-tooltip>
            </span>
          </p>
        </div>
        <div class="sr-all" @click="handle($event)" style="cursor: pointer; margin-top: 20px; margin-left: 40px">
          <div class="sra-content" v-for="(item, index) in searchResult" :key="index">
            <ul>
              <li :data-id="item.id" style="color: rgba(0, 84, 190, 1)">
                <span v-html="formatting(item.formulaPlain)"></span>-({{
            item.casnum
          }})-{{ item.nameEn }}-{{ item.nameCn }}
              </li>
            </ul>
            <img :src="getPic(item.formulaPic)" alt="" class="molcuPic" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getVocData, getMoleculePic, getPic } from "@/api/publicSearch";
import { goWhere } from "@/api/formatVoc";
import { formatting } from "@/api/formatVoc";
export default {
  name: "SearchView",
  data() {
    return {
      //传入下一级标题展示
      searchVocDetails: "",
      gotoNew: false,
      voc: "",
      //搜索结果数量
      resultNum: 0,
      //搜索框绑定变量
      searchVoc: "",
      //存放同分异构
      searchResult: [],
      //选择后的vocId
      vocId: "",
      radio: "催化反应",
      show: true,
      //传入下个页面中voc名字
      vocNext: "",
    };
  },
  watch: {
    radio: {
      handler(newVal) {
        this.$store.dispatch("changeRadio", newVal);
      },
    },
    voc: {
      handler(newVal) {
        this.$store.dispatch("changeVoc", newVal);
      },
    },
    searchResult: {
      handler(newVal) {
        this.$store.dispatch("changeResult", newVal);
      },
    },
    resultNum: {
      handler(newVal) {
        this.$store.dispatch("changeNum", newVal);
      },
    },
  },
  mounted() {
    this.loading = true;
    this.voc = this.$store.state.voc;
    this.radio = this.$store.state.radio;
    this.searchResult = this.$store.state.searchResult;
    this.resultNum = this.$store.state.resultNum;
    getVocData({ formulaPlain: this.voc }).then((res) => {
      this.searchResult = res.data.data;
      this.resultNum = this.searchResult.length;
      this.loading = false;
    });
  },
  methods: {
    // handle(e) {
    //   let target = e.target.parentNode.firstChild.innerHTML;
    //   // console.log(e.target);
    //   if (target[0] !== "<") {
    //     this.vocId = e.target.dataset.id;
    //     this.searchVocDetails = target;
    //     this.gotoNew = true;
    //     this.gotoNewpage();
    //   }
    // },
    handle(e) {
      console.log(
        e.target.innerText.substring(0, e.target.innerText.indexOf("-"))
      );
      //let target = e.target.parentNode.firstChild.innerHTML;
      this.vocId = e.target.dataset.id;
      // this.searchVocDetails = target;
      this.searchVocDetails = e.target.childNodes[1].data;
      this.vocNext = e.target.innerText.substring(
        0,
        e.target.innerText.indexOf("-")
      );
      this.gotoNew = true;
      this.gotoNewpage();
    },
    gotoNewpage() {
      let path = goWhere(this.radio);
      this.$router.push({
        path: path,
        query: {
          searchVocDetails: this.searchVocDetails,
          vocId: this.vocId,
          vocNext: this.vocNext,
          radio: this.radio,
        },
      });
    },
    //搜索框搜索
    search() {
      this.loading = true;
      getVocData({ formulaPlain: this.searchVoc }).then((res) => {
        this.searchResult = res.data.data;
        this.voc = this.searchVoc;
        this.resultNum = this.searchResult.length;
        this.loading = false;
      });
    },

    searchGas() {
      console.log(this.searchGasDetails);
      const ifgo = this.searchGasDetails.some((item) => item === "qirongjiao");
      if (ifgo) {
        this.$router.push({
          path: "/Aerosol",
          // query: {
          //   voc: this.searchVocDetails,
          // },
        });
      }
    },
    //获取图片
    getPic(str) {
      return getMoleculePic(str);
    },
    //格式化
    formatting(str) {
      return formatting(str);
    },
    //获取图片
    getPic2(str) {
      return getPic(str);
    },
  },
};
</script>

<style scoped lang="scss">
@import "../css/common.scss";

.g-main {
  display: block;
}

.centerDiv {
  margin: 0 auto;
}

.search-details {
  width: 35%;
  text-align: left;
  font-size: #{$second_font_size};
  font-weight: #{$second_font_weight};
  margin-top: #{$second_titile_top}px;
  margin-left: #{$second_titile_left}px;
  margin-bottom: #{$second_titile_bottom}px;
}

.search-result {
  width: 35%;
  // margin: 0 auto;
  background-color: #ffffff;
  margin-top: #{$second_titile_top}px;
  // margin-left: #{$second_titile_left}px;
  // border: 1px solid black;
  // border-radius: 10px; /* 设置圆角半径为 10 像素 */
  // box-shadow: 0 0 10px rgba(0, 0, 0, 0.6); /* 设置阴影效果 */
}

.sd-title {
  text-align: left;
  // margin-left: 10px;
  margin-top: 10px;
  margin-bottom: 20px;
  font-size: small;
}

.sr-title {
  text-align: left;
  font-size: #{$first_font_size};
  font-weight: #{$first_font_weight};
  margin-top: #{$first_titile_top}px;
  margin-left: #{$first_titile_left}px;
  margin-bottom: #{$first_titile_bottom}px;
}

.sr-all {

  // display: flex;
  // flex-wrap: wrap;
  img {
    width: 100px;
    margin-left: 30px;
  }

  .sra-content {
    text-align: left;
    font-size: #{$second_font_size};
    font-weight: #{$second_font_weight};
    margin-top: #{$second_titile_top}px;
    margin-left: #{$second_titile_left}px;
    margin-bottom: #{$second_titile_bottom}px;
  }

  .sra-content li {
    &:hover {
      color: rgba(0, 84, 190, 1);
    }
  }
}

.radio {
  margin-top: 30px;
  padding-left: 50px;
  text-align: left;
}

.el-cascader {
  margin-top: 10px;
  width: 100%;
}

.el-cascader-menu {
  min-width: 350px !important;
}

// .cascader-content>div{
//   display: inline-block;
// }
.cascader-content {
  display: flex;
  // align-items: center;
}

.cascader-content-one {
  width: 458px;
  // flex-grow: 1;
}

.cascader-content-two {
  margin-top: 10px;
}

.cct-button {
  border-radius: 0 3px 3px 0;
  background: #f5f7fa;
}

.page-title {
  text-align: left;
  font-size: #{$first_font_size};
  font-weight: #{$first_font_weight};
  margin-top: #{$first_titile_top}px;
  // margin-left: #{$first_titile_left}px;
  margin-bottom: #{$first_titile_bottom}px;
}

.sr-ul li {
  margin-bottom: 15px;
}

.radio-choose {
  margin-top: 10px;
  max-width: 418px;

  .el-radio {
    margin-top: 10px;
    margin-bottom: 10px;
    width: 110px;
    font-size: x-large;
  }
}

// .p-result {
//   text-align: left;
//   font-size: #{$second_font_size};
//   font-weight: #{$second_font_weight};
//   margin-top: #{$second_titile_top}px;
//   margin-left: #{$second_titile_left}px;
//   margin-bottom: #{$second_titile_bottom}px;
// }

//设置选择按钮大小
::v-deep .el-radio__label {
  font-size: medium;
}

//设置按钮选中后颜色
::v-deep .el-radio__input.is-checked+.el-radio__label {
  color: rgba(0, 84, 190, 1);
}

// 搜索框
::v-deep .el-input-group__append,
.el-input-group__prepend {
  background-color: #4295e3;
  color: #ffffff;
  border: 0px solid #dcdfe6;
  width: 20px;
  text-align: center;
}
</style>
